package org.jamwiki.servlets;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jamwiki.WikiBase;
import org.jamwiki.WikiException;
import org.jamwiki.WikiMessage;
import org.jamwiki.authentication.WikiUserAuth;
import org.jamwiki.model.Role;
import org.jamwiki.model.Watchlist;
import org.jamwiki.utils.Pagination;
import org.jamwiki.utils.WikiLogger;
import org.jamwiki.utils.WikiUtil;
import org.springframework.web.servlet.ModelAndView;

public class CreateTopicServlet extends JAMWikiServlet {
	
	private static final WikiLogger logger = WikiLogger.getLogger(CreateTopicServlet.class.getName());
	protected static final String CREATTOPIC = "createTopic.jsp";
	protected static final String JSP_WATCHLIST = "watchlist.jsp";
	

	@Override
	protected ModelAndView handleJAMWikiRequest(HttpServletRequest request,
			HttpServletResponse response, ModelAndView next,
			WikiPageInfo pageInfo) throws Exception {
		    String actionType = request.getParameter("actionType");
		    if("public".equalsIgnoreCase(actionType)){  
		    	String id = request.getParameter("topicId"); 
		    	WikiBase.getDataHandler().updateTopicAuditStatus(Integer.parseInt(id), null);
		    	String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
		    	 
				Pagination pagination = ServletUtil.loadPagination(request, next);
			 
				Collection items = WikiBase.getDataHandler().getTopicsPublic(virtualWiki, pagination);
				next.addObject("itemCount", new Integer(items.size()));
				next.addObject("items", items);
				next.addObject("rootUrl", "Special:PublicList");
				pageInfo.setPageTitle(new WikiMessage("public.title"));
				pageInfo.setContentJsp("middle.jsp");
				pageInfo.setSpecial(true);
		    }else if("addwatch".equals(actionType)){
		    	String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
		    	String topicName = request.getParameter("topicName");
		    	WikiUserAuth user = ServletUtil.currentUser();
		    	
		    	Watchlist watchlist = ServletUtil.currentWatchlist(request, virtualWiki); 
				if (!watchlist.containsTopic(topicName)) {
					WikiBase.getDataHandler().writeWatchlistEntry(watchlist, virtualWiki, topicName, user.getUserId(), null);
				}
				Pagination pagination = ServletUtil.loadPagination(request, next);
				view(request, next, pageInfo);
				
		    }else{
			    pageInfo.setPageTitle(new WikiMessage("createtopic.title"));
			    pageInfo.setContentJsp(CREATTOPIC);
				pageInfo.setSpecial(true);
		    }
		return next;
	}
	/**
	 *
	 */
	private void view(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo) throws Exception {
		String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
		Pagination pagination = ServletUtil.loadPagination(request, next);
		WikiUserAuth user = ServletUtil.currentUser();
		if (!user.hasRole(Role.ROLE_USER)) {
			throw new WikiException(new WikiMessage("watchlist.error.loginrequired"));
		}
		Collection changes = WikiBase.getDataHandler().getWatchlist(virtualWiki, user.getUserId(), pagination);
		next.addObject("numChanges", new Integer(changes.size()));
		next.addObject("changes", changes);
		pageInfo.setPageTitle(new WikiMessage("watchlist.title"));
		pageInfo.setContentJsp(JSP_WATCHLIST);
		pageInfo.setSpecial(true);
	}
}
